#lang r5rs
(define (make-mobile left right)
  (list left right))
(define (make-branch length structure)
  (list length structure))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (left-branch binary)
  (car binary))
(define (right-branch binary)
  (car (cdr binary)))
(define (branch-length branch)
  (car branch))
(define (branch-structure branch)
  (car (cdr branch)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (total-weight x)

    (define (structure-weight x)
      (cond (((null? x) 0)
             ((not(pair? (branch-structure x))) (branch-structure x))
             ((pair? (branch-structure x)) (total-weight (branch-structure x))))))
  (+ (structure-weight (left-branch x)) (structure-weight (right-branch x)))
  )
(define (balance? x)
  
    (define (structure-weight x)
      (cond (((null? x) 0)
             ((not(pair? (branch-structure x))) (branch-structure x))
             ((pair? (branch-structure x)) (total-weight (branch-structure x))))))
  (= (* (structure-weight (left-branch x)) (branch-length (left-branch x))) (* (structure-weight (right-branch x)) (branch-length (right-branch x))))
  )